package com.daxia.bumall.model;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Vector;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import com.daxia.bumall.basemodel.CoreUser;
import com.daxia.bumall.common.UserType;
import com.daxia.bumall.dto.UserDTO;
import com.daxia.bumall.service.DesignerService;
import com.daxia.bumall.service.UserService;
import com.daxia.bumall.support.SpringContextHolder;
import com.daxia.bumall.util.MyWebUtils;

/**
 * Specification
 */
@Entity
@Table(name = "specification") 
public class Specification extends BaseModel {
	/**
	 * id
	 */
	@Id
	@GeneratedValue
	@Column(name = "id")
    private Long id;
	/**
	 * 产品
	 */
	@ManyToOne
	@JoinColumn(name = "product_id")
    private Product product;
	/**
	 * 工厂编码
	 */
	@Column(name = "factoryCode")
    private String factoryCode;
	private String ogCode;
	private String images;
	/**
	 * 规格1
	 */
	@Column(name = "spec1")
    private String spec1;
	/**
	 * 规格2
	 */
	@Column(name = "spec2")
    private String spec2;
	/**
	 * 销售价
	 */
	@Column(name = "sellingprice")
    private Double sellingPrice;
	/**
	 * 实际的价格，也就是上浮之后的价格，这个字段不跟数据库关联
	 */
	@Transient
	private Double realPrice;
	/**
	 * 市场价
	 */
	@Column(name = "marketprice")
    private Double marketPrice;
	/**
	 * 重量
	 */
	@Column(name = "weight")
    private String weight;
	/**
	 * 体积
	 */
	@Column(name = "size")
    private String size;
	/**
	 * 库存
	 */
	@Column(name = "stock")
    private Integer stock;
	
	/**
	 * 序号
	 */
	@Column(name = "seq")
	private Integer seq;
	
	@OneToMany(mappedBy = "specification",  cascade=CascadeType.REMOVE)
	private List<ProductProperties> productProperties;
	
	public List<String> getSpecImages() {   //得到对应的规格图片集合
		List<String> imageList = new Vector<String>();
		String imgstr = this.getImages();
		if(imgstr != null) {
			String[] imgs = imgstr.split(",");
			for(String theImg:imgs) {
				if(StringUtils.isNotBlank(theImg)) {
					imageList.add(theImg);
				}
			}
		}
		return imageList;
	}
	
	public Long getId() {
    	return id;
    }
	
    public void setId(Long id) {
    	this.id = id;
    }
	
	public Product getProduct() {
    	return product;
    }
	
    public void setProduct(Product product) {
    	this.product = product;
    }
	
	public String getFactoryCode() {
    	return factoryCode;
    }
	
    public void setFactoryCode(String factoryCode) {
    	this.factoryCode = factoryCode;
    }
	
	public String getSpec1() {
    	return spec1;
    }
	
    public void setSpec1(String spec1) {
    	this.spec1 = spec1;
    }
	
	public String getSpec2() {
    	return spec2;
    }
	
    public void setSpec2(String spec2) {
    	this.spec2 = spec2;
    }
	
	public Double getSellingPrice() {
    	return sellingPrice;
    }
	
    public void setSellingPrice(Double sellingPrice) {
    	this.sellingPrice = sellingPrice;
    }
	
	public Double getMarketPrice() {
    	return marketPrice;
    }
	
    public void setMarketPrice(Double marketPrice) {
    	this.marketPrice = marketPrice;
    }
	
	public String getWeight() {
    	return weight;
    }
	
    public void setWeight(String weight) {
    	this.weight = weight;
    }
	
	public String getSize() {
    	return size;
    }
	
    public void setSize(String size) {
    	this.size = size;
    }
	
	public Integer getStock() {
    	return stock;
    }
	
    public void setStock(Integer stock) {
    	this.stock = stock;
    }

	public String getOgCode() {
		return ogCode;
	}

	public void setOgCode(String ogCode) {
		this.ogCode = ogCode;
	}

	public String getImages() {
		if (StringUtils.isBlank(images)) {
			return "";
		}
		return images;
	}

	public void setImages(String images) {
		this.images = images;
	}
	
	public Integer getSeq() {
		return seq;
	}

	public void setSeq(Integer seq) {
		this.seq = seq;
	}

	public Double getRealPrice() {
		Double d1 = doCal();
		if (d1 == null) {
			return null;
		}
		
		BigDecimal deSource = new BigDecimal(d1);
		deSource = deSource.setScale(0,BigDecimal.ROUND_HALF_UP);
		DecimalFormat df=new DecimalFormat("#.00"); 
		return Double.parseDouble(df.format(deSource.doubleValue())); 
	}
	public static double getFactor2() {
		UserService userService = SpringContextHolder.getBean(UserService.class);
		CoreUser coreUser = (CoreUser) MyWebUtils.getCurrentRequest().getSession().getAttribute("currentUser");
		if (coreUser == null) { // 没有登陆
			return 2d;
		}
		
		UserDTO user = userService.load(coreUser.getId());
		// 如果用户本身的不为空就返回本身的
		if (user.getPriceFactor() != null) {
			return user.getPriceFactor();
		}
		// 如果部门不为空
		if (user.getDepartment() != null && user.getDepartment().getPriceFactor() != null) {
			return user.getDepartment().getPriceFactor();
		}
		Double factor = null; //userService.load(getProduct().getUser().getId()).getPriceFactor();
		if (factor == null) {
			factor = 1d;
		}
		if (user.getType() == null) { // 未知类型
			return factor * 2;
		}
		if (user.getType() == UserType.City.getValue()) {
			return factor;
		}
		if (user.getType() == UserType.DecorateCompany.getValue()) {
			return factor * 1.06;
		}
		if (user.getType() == UserType.Designer.getValue()) {
			// 如果是城运商的增加的设计师，为1.15，否则就是c端价
			// 找到设计师
			DesignerService designerService = SpringContextHolder.getBean(DesignerService.class);
			Designer d = designerService.findByUser(user.getId());
			if (d.getCreator() != null && d.getCreator().getType() == UserType.City.getValue()) {
				return factor * 1.15;
			}
			return factor * 2;
		}
		if (user.getType() == UserType.Normal.getValue()) {
			return factor * 2;
		}
		if (user.getType() == UserType.Manager.getValue()) {
			return 1d; // 管理员看到出厂价
		}
		return factor * 2; // 默认就是2倍	
	}
	public Double getFactor() {
		UserService userService = SpringContextHolder.getBean(UserService.class);
		CoreUser coreUser = (CoreUser) MyWebUtils.getCurrentRequest().getSession().getAttribute("currentUser");
		if (coreUser == null) { // 没有登陆
			return 2d;
		}
		
		UserDTO user = userService.load(coreUser.getId());
		// 如果用户本身的不为空就返回本身的
		if (user.getPriceFactor() != null) {
			return user.getPriceFactor();
		}
		// 如果部门不为空
		if (user.getDepartment() != null && user.getDepartment().getPriceFactor() != null) {
			return user.getDepartment().getPriceFactor();
		}
		Double factor = userService.load(getProduct().getUser().getId()).getPriceFactor();
		if (factor == null) {
			factor = 1d;
		}
		if (user.getType() == null) { // 未知类型
			return factor * 2;
		}
		if (user.getType() == UserType.City.getValue()) {
			return factor;
		}
		if (user.getType() == UserType.DecorateCompany.getValue()) {
			return factor * 1.06;
		}
		if (user.getType() == UserType.Designer.getValue()) {
			// 如果是城运商的增加的设计师，为1.15，否则就是c端价
			// 找到设计师
			DesignerService designerService = SpringContextHolder.getBean(DesignerService.class);
			Designer d = designerService.findByUser(user.getId());
			if (d.getCreator() != null && d.getCreator().getType() == UserType.City.getValue()) {
				return factor * 1.15;
			}
			return factor * 2;
		}
		if (user.getType() == UserType.Normal.getValue()) {
			return factor * 2;
		}
		if (user.getType() == UserType.Manager.getValue()) {
			return 1d; // 管理员看到出厂价
		}
		return factor * 2; // 默认就是2倍	
	}
	private Double doCal() {
		try {
		if (getSellingPrice() == null) {
			return getSellingPrice();
		}
		if (MyWebUtils.getCurrentRequest() == null) {
			return getSellingPrice();
		}
		
		return getSellingPrice() * getFactor();
		
		} catch (Exception e) {
			return null;
		}
	}

	public void setRealPrice(Double realPrice) {
		this.realPrice = realPrice;
	}

	public List<ProductProperties> getProductProperties() {
		return productProperties;
	}

	public void setProductProperties(List<ProductProperties> productProperties) {
		this.productProperties = productProperties;
	}
	
}
